package com.yujistu.widget;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.Display;
import android.widget.RelativeLayout;

/**
 * Created by S3YOGA on 2015/11/3.
 */
public class InputMethodRelativeLayout extends RelativeLayout {

    private int width;
    protected OnSizeChangeListener onSizeChangeListener;
    private boolean sizeChanged = false; //变化的标志
    private int height;
    private int screenWidth; //屏幕宽度
    private int screentHeight; //屏幕高度

    public InputMethodRelativeLayout(Context paramContext) {
        super(paramContext);
    }

    public InputMethodRelativeLayout(Context paramContext, AttributeSet paramAttributeSet) {
        super(paramContext, paramAttributeSet);
        Display localDisplay = ((Activity)paramContext).getWindowManager().getDefaultDisplay();
        this.screenWidth = localDisplay.getWidth();
        this.screentHeight = localDisplay.getHeight();
    }

    public InputMethodRelativeLayout(Context paramContext, AttributeSet paramAttributeSet, int paramInt) {
        super(paramContext, paramAttributeSet, paramInt);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        this.width =  widthMeasureSpec;
        this.height = heightMeasureSpec;
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        //监听不为空、宽度不变、当前高度与历史高度不为0
        if((this.onSizeChangeListener != null) && (w == oldw) && (oldw != 0) && (oldh != 0)){
            if((h >= oldh) || (Math.abs(h - oldh) <= 1 * this.screentHeight / 4)){
               if((h <= oldh) || (Math.abs(h - oldh) <= 1 * this.screentHeight / 4))
                   return;
                this.sizeChanged = false;
            }else{
                this.sizeChanged = true;
            }
            this.onSizeChangeListener.onSizeChange(this.sizeChanged, oldh, h);
            measure(this.width-w+getWidth(), this.height - h + getHeight());
        }
    }

    /**
     * 设置监听事件
     *
     * @param onSizeChangeListener
     */
    public void setOnSizeChangeListener(OnSizeChangeListener onSizeChangeListener) {
        this.onSizeChangeListener = onSizeChangeListener;
    }

    /**
     * 大小改变的内部接口
     */
    public interface OnSizeChangeListener{
        public abstract void onSizeChange(boolean paramBoolean, int w, int h);
    }
}
